package com.meng.mini.spring.beans;

import com.meng.mini.spring.core.Resource;
import org.dom4j.Element;

/**
 * 用于 将 xml  解析为 beanDefinition
 * @author Meng Xu
 * @version 1.0.0
 * @date 2024-01-16 10:35
 **/
public class XmlBeanDefinitionReader {

    SimpleBeanFactory simpleBeanFactory;

    public XmlBeanDefinitionReader(SimpleBeanFactory simpleBeanFactory){
        this.simpleBeanFactory = simpleBeanFactory;
    }

    /**
     * 通过 resource 加载 bean 资源信息(beanDefinition)
     * @param resource 资源对象
     */
    public void loadBeanDefinitions(Resource resource){
        while (resource.hasNext()) {
            //获取 xml 中的bean信息
            Element element = (Element) resource.next();
            String beanId = element.attributeValue("id");
            String beanClassName = element.attributeValue("class");
            //根据bean信息创建 BeanDefinition
            BeanDefinition beanDefinition = new BeanDefinition(beanId, beanClassName);
            //注册 beanDefinition
            this.simpleBeanFactory.registerBeanDefinition(beanId,beanDefinition);
        }

    }






}
